package ui;

import server.CheckConn;
import server.HandleConn;
import server.Out;
import server.ServerMain;

import java.util.Date;

public class CountDown extends Thread{
    @Override
    public void run() {
        //ServerMain.mf.start.setEnabled(false);
        //这里是一个抢答环节的生命周期管理
        ServerMain.mf.panel.idToDisplay=0;
        //设置所有未犯规
        for(HandleConn conn:ServerMain.checkConn.conns){
            conn.rushable=true;
            conn.rushtime=-1;
        }
        //设置未抢答
        CheckConn.acceptRush=true;
        //进入倒计时状态
        ServerMain.mf.panel.stateStr="倒计时，请勿抢答";

        ServerMain.state=ServerMain.COUNT;
        ServerMain.mf.panel.countDownTime=3;
        ServerMain.mf.repaint();
        PlayAudio.play("1.wav");
        sleep0(1000);

        //下一秒
        ServerMain.mf.panel.countDownTime=2;
        ServerMain.mf.repaint();
        sleep0(1000);
        //下一秒
        ServerMain.mf.panel.countDownTime=1;
        ServerMain.mf.repaint();
        sleep0(1000);
        //开始
        ServerMain.mf.panel.countDownTime=0;
        ServerMain.mf.repaint();
        ServerMain.mf.panel.stateStr=">抢答开始<";
        ServerMain.state=ServerMain.RUSH;
        Out.say("rush");
        CheckConn.startTime=new Date().getTime()+90;

        PlayAudio.play("3.wav");


        //超时
        sleep0(ServerMain.rushTimeOut);
        Out.say("stop");
        ServerMain.state=ServerMain.FREE;
        CheckConn.acceptRush=false;
        ServerMain.checkConn.computeFirstRusher();



        //ServerMain.mf.panel.stateStr="开始后的延迟是为了统计选手的抢答信息并进行比较";
        ServerMain.mf.repaint();

        ServerMain.mf.sb.setEnabled(true);


        /*for(HandleConn conn:ServerMain.checkConn.conns){
            conn.rushable=true;
            //conn.rushtime=-1;
        }*/
    }
    void sleep0(long mile){
        try {
            this.sleep(mile);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
